package com.ambitor.ipro.actions;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang.StringUtils;

import com.ambitor.grass.util.data.DataMap;
import com.ambitor.grass.util.data.IData;
import com.ambitor.ipro.util.Constants;

public class SettingPersistAction extends BaseAction {

	private static final long	serialVersionUID	= -2528418471608896211L;

	private Map<String, String>	ipro;

	public SettingPersistAction() {
	}

	public Map<String, String> getIpro() {
		return ipro;
	}

	public void setIpro(Map<String, String> ipro) {
		this.ipro = ipro;
	}

	@Override
	@SuppressWarnings("unchecked")
	public String execute() throws UnsupportedEncodingException {
		if (ipro != null) {
			String encodedTitle = ipro.get("promote_title");
			if (encodedTitle != null)
				ipro.put("promote_title", URLDecoder.decode(encodedTitle,
						Constants.APPLICATION_ENCODING));
		}

		Object obj = pd.getSessionAttribute(Constants.PRO_SETTING);
		if (obj == null) {
			pd.setSessionAttribute(Constants.PRO_SETTING, init(ipro));

		}
		else {
			Map<String, String> oldProInSession = (Map<String, String>) obj;
			if (ipro != null)
				oldProInSession.putAll(ipro);
			pd.setSessionAttribute(Constants.PRO_SETTING, oldProInSession);

		}
		return SUCCESS;
	}

	@SuppressWarnings("unchecked")
	private Map<String, String> init(Map<String, String> ipro) {
		IData data = new DataMap(ipro);
		data.put("intelligence_num_1", data.getString("intelligence_num_1",
				Constants.DEFAULT_INTELLIGENCE_NUMBER));
		data.put("intelligence_num_2", data.getString("intelligence_num_2",
				Constants.DEFAULT_INTELLIGENCE_NUMBER));
		data.put("intelligence_label_1", data.getString("intelligence_label_1",
				Constants.DEFAULT_INTELLIGENCE_LABEL_1));
		data.put("intelligence_label_2", data.getString("intelligence_label_2",
				Constants.DEFAULT_INTELLIGENCE_LABEL_2));
		data.put("intelligence_frequency", data.getString(
				"intelligence_frequency",
				Constants.DEFAULT_INTELLIGENCE_FREQUENCY));

		data.put("promote_radio", data.getString("promote_radio",
				Constants.DEFAULT_PROMOT_RADIO));
		data.put("promote_style", data.getString("promote_style",
				Constants.DEFAULT_PROMOT_STYLE));
		data.put("buy_button", data.getString("buy_button",
				Constants.DEFAULT_BUY_BUTTON));
		data.put("price_label", data.getString("price_label",
				Constants.DEFAULT_PRICE_LABEL));
		data.put("promote_title", data.getString("promote_title",
				Constants.DEFAULT_PROMOT_TITLE));
		data.put("to_be_promoted", data.getString("to_be_promoted",
				StringUtils.EMPTY));
		data.put("to_promote", data.getString("to_promote", StringUtils.EMPTY));
		data.put("num_per_line", data.getString("num_per_line",
				Constants.DEFAULT_NUM_PER_LINE));
		data.put("install_place", data.getString("install_place",
				Constants.DEFAULT_INSTALL_PLACE));
		return new HashMap(data);
	}
}
